package org.example.view;

import org.example.controller.IndexController;
import org.example.data.Account;
import org.example.data.User;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.List;

public class Index extends JFrame {
    private JButton changePasswordButton, manageIncomeButton;
    private JPanel headerPanel, queryPanel, tablePanel;
    private JComboBox<String> typeComboBox;
    private JTextField startDateField, endDateField;
    private JButton queryButton;
    private JLabel totalAmountLabel;
    private JTable infoTable;
    private User user;
    private List<Account> accounts;
    private DefaultTableModel model;


    public Index(User user) {
        this.user = user;
        setTitle("欢迎" + user.getUsername() + "使用个人账本");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(800, 600);
        setLocationRelativeTo(null);

        // 设置整体布局为 BoxLayout 垂直排列
        setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));

        // 添加头部按钮
        headerPanel = new JPanel();
        changePasswordButton = new JButton("修改密码");
        manageIncomeButton = new JButton("收支管理");
        headerPanel.add(changePasswordButton);
        headerPanel.add(manageIncomeButton);
        add(headerPanel);

        // 添加查询条件输入
        queryPanel = new JPanel();
        typeComboBox = new JComboBox<>(new String[]{"收入", "支出", "全部"});
        startDateField = new JTextField(10);
        endDateField = new JTextField(10);
        queryButton = new JButton("查询");
        queryPanel.add(new JLabel("选择收支类型:"));
        queryPanel.add(typeComboBox);
        queryPanel.add(new JLabel("选择起始日期:"));
        queryPanel.add(startDateField);
        queryPanel.add(new JLabel("选择终止日期:"));
        queryPanel.add(endDateField);
        queryPanel.add(queryButton);
        add(queryPanel);
        IndexController indexController = new IndexController();
        accounts = indexController.getCheck(user.getId()).getData();

        // 显示个人总收支金额
        Double money = getAmount();
        totalAmountLabel = new JLabel("个人总收支金额: " + money);
        totalAmountLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(totalAmountLabel);

        // 明细信息表格
        tablePanel = new JPanel(new BorderLayout());
        infoTable = new JTable(new DefaultTableModel(new Object[]{"编号", "日期", "类型", "内容", "金额"}, 0));
        JScrollPane scrollPane = new JScrollPane(infoTable);
        tablePanel.add(scrollPane, BorderLayout.CENTER);
        add(tablePanel);
        showAccountsInTable();

        actionListener();

        setVisible(true);
    }

    private void actionListener() {
        changePasswordButton.addActionListener(e -> {
            new UpdatePassword(user);
        });

        manageIncomeButton.addActionListener(e -> {
            new ManageIncome(user);
            totalAmountLabel.setText("个人总收支金额: " + getAmount());
            updateTable();
        });
        queryButton.addActionListener(e -> {
            String startDate = startDateField.getText();
            String endDate = endDateField.getText();
            String type = (String) typeComboBox.getSelectedItem();
            if (startDate.isEmpty()) {
                startDate ="1800-1-1 00:00:00";
            }
            if (endDate.isEmpty()) {
                endDate = "3000-12-31 23:59:59";
            }
            if ("全部".equals(type)) {
                accounts = new IndexController().query(startDate, endDate).getData();
            } else {
                accounts = new IndexController().query(startDate, endDate, type).getData();
                System.out.println(accounts.size());
            }
            updateTable(); // 更新表格数据
        });
    }

    private Double getAmount() {
        Double totalAmount = 0.0;
        for (Account account : accounts) {
           if (account.getType().equals("收入")){
               totalAmount += account.getAmount();
           }else {
               totalAmount -= account.getAmount();
           }
        }
        return totalAmount;
    }
    private void showAccountsInTable() {
        model = (DefaultTableModel) infoTable.getModel();
        for (Account account : accounts) {
            model.addRow(new Object[]{account.getId(), account.getDate(), account.getType(), account.getContent(), account.getAmount()});
        }
    }
    private void updateTable(){
        model.setRowCount(0); // 清空表格数据
        for (Account account : accounts) {
            model.addRow(new Object[]{account.getId(), account.getDate(), account.getType(), account.getContent(), account.getAmount()});
        }
        model.fireTableDataChanged(); // 刷新表格数据
    }

}